package com.cc.moxingvoice.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.cc.moxingvoice.BR;
import com.cc.moxingvoice.R;
import com.cc.moxingvoice.model.SingleTextItem;
import com.cc.moxingvoice.model.VoiceItem;
import com.cc.moxingvoice.utils.FilesUtil;
import com.superc.cframework.base.ui.CBaseRecyclerViewAdapter;
import com.superc.cframework.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2018/3/3 on 22:36
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class ButtonTwoListDialog extends Dialog implements View.OnClickListener {

    public static final int BUTTON = -1;

    private TextView titleTxt;
    private TextView rightTxt;
    private TextView leftTxt;

    private Context mContext;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;

    private RecyclerView mRecyclerView;
    private List<SingleTextItem> list;
    private ButtonTwoListAdapter mAdapter;

    public ButtonTwoListDialog(Context context) {
        super(context);
        this.mContext = context;
        this.list = new ArrayList<SingleTextItem>();
    }

    public ButtonTwoListDialog(Context context, int themeResId, List<SingleTextItem> list) {
        super(context, themeResId);
        this.mContext = context;
        this.list = list;
    }

    public ButtonTwoListDialog(Context context, int themeResId, List<SingleTextItem> list, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.list = list;
        this.listener = listener;
    }

    protected ButtonTwoListDialog(Context context, boolean cancelable, List<SingleTextItem> list, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        this.list = list;
    }

    public ButtonTwoListDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public ButtonTwoListDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public ButtonTwoListDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_button_two_list);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv);
        titleTxt = (TextView) findViewById(R.id.title);
        rightTxt = (TextView) findViewById(R.id.right);
        rightTxt.setOnClickListener(this);
        leftTxt = (TextView) findViewById(R.id.left);
        leftTxt.setOnClickListener(this);

        if (!TextUtils.isEmpty(positiveName)) {
            rightTxt.setText(positiveName);
        }

        if (!TextUtils.isEmpty(negativeName)) {
            leftTxt.setText(negativeName);
        }

        if (!TextUtils.isEmpty(title)) {
            titleTxt.setText(title);
        }

        initList();
    }

    private void initList() {
        LinearLayoutManager ms = new LinearLayoutManager(mContext);
        mRecyclerView.setLayoutManager(ms);
        mAdapter = new ButtonTwoListAdapter(getContext(), list, R.layout.item_single_text, BR.item);
        mRecyclerView.setAdapter(mAdapter); // 这里或者在xml里设置adapter
        mAdapter.setOnItemClickListener(new CBaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewDataBinding binding, int position) {
                if (0 == position) {
                    addNewLabel();
                    dismiss();
                    return;
                }
                if (listener != null) {
                    listener.onClick(ButtonTwoListDialog.this, true, position);
                }
            }
        });
        mAdapter.notifyDataSetChanged();
    }

    private void addNewLabel() {
        new ButtonTwoEditDialog(getContext(), R.style.dialog, new ButtonTwoEditDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm, String content) {
                if (confirm) {
                    if (content.isEmpty()) {
                        ToastUtil.showShort(getContext(), mContext.getString(R.string.input_label_null));
                        return;
                    }
                    if (content.equals(getContext().getString(R.string.add_new_label))
                            || content.equals(getContext().getString(R.string.add_new_label))) {
                        ToastUtil.showShort(getContext(), mContext.getString(R.string.this_name_not_available));
                        return;
                    }
                    list.add(new SingleTextItem(content));
                    if (listener != null) {
                        listener.onClick(ButtonTwoListDialog.this, true, list.size() - 1);
                    }
                    dialog.dismiss();
                }
            }
        }).setHint(mContext.getString(R.string.input_label_name)).setTitle(mContext.getString(R.string.hint)).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                if (listener != null) {
                    listener.onClick(this, false, BUTTON);
                }
                this.dismiss();
                break;
            case R.id.right:
                if (listener != null) {
                    listener.onClick(this, true, BUTTON);
                }
                break;
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm, int position);
    }

}
